/*
 * XmlStatistics.java
 *
 * Created on Sobota, 2007, november 17, 12:40
 *
 */

package xml;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import logic.Statistic;
import org.dom4j.*;
import org.dom4j.io.SAXReader;

/**
 *
 * @author abracadabuda
 */
public class XmlStatistics {
    
    private static Document document;
    private static Element root;
    
    /**
     * vytvori XML dokument a nastavi mu rootElement
     */
    
    private static void createXmlDocument() {
        document = DocumentHelper.createDocument();
        
        root = document.addElement( "Statistics" );
        root.addText("\n");
    }
    
    /**
     * ulozi XML dokument do suboru
     *
     * @param path absolutna adresa, kam sa ma dokument ulozit
     * @throws IOException ak sa nepodari zapisat subor
     */
    
    private static void saveXmlDocument(String path) throws IOException {
        FileWriter out;
        out = new FileWriter( path );
        document.write( out );
        out.close();
    }
    
    /**
     * ulozi kolekciu statistik na zadanu cestu, uklada iba priebezne statistiky, takze sa neuklada stdDev
     *
     * @param st subor statistik
     * @param path absolutna cesta k suboru, kam sa ma ulozit
     * @throws IOException ak sa nepodari zapisat subor
     */
    
    public static void saveSimulationRunResults(LinkedList<Statistic> st, String path) throws IOException {
        createXmlDocument();
        
        for (Statistic stat : st) {
            Element statElement = root.addElement( "statistic" );
            statElement.addAttribute( "type", String.valueOf(stat.getType()));
            statElement.addAttribute( "entityName", stat.getEntityName());
            statElement.addAttribute( "meanValue", String.valueOf(stat.getMeanValue()));
            root.addText("\n");
        }
        
        saveXmlDocument(path);
    }
    
    /**
     * nacita zo zadanej cesty ulozene statistiky
     *
     * @param path absollutna cesta k suboru, z ktoreho sa maju nacitavat
     * @return subor statistik zo suboru
     * @throws DocumentException ak nastala chyba pri citani dokumentu
     */
    
    public static LinkedList<Statistic> loadSimulationRunResults(String path) throws DocumentException {
        SAXReader reader = new SAXReader();
        document = reader.read(path);
        
        root = document.getRootElement();
        
        LinkedList<Statistic> listStatistic = new LinkedList<Statistic>();
        
        for (Iterator iterStat = root.elementIterator( "statistic" ); iterStat.hasNext() ; ) {
            Element statElement = (Element) iterStat.next();
            
            Statistic stat = new Statistic( Integer.parseInt(statElement.attributeValue( "type" )),
                    statElement.attributeValue( "entityName"),
                    Double.parseDouble(statElement.attributeValue( "meanValue" )));
            listStatistic.add(stat);
        }
        return listStatistic;
    }
    
    /**
     * ulozi vysledne statistiky do suboru s cestou
     *
     * @param st subor statistik na ulozenie
     * @param path absolutna cesta k suboru, kam sa maju staitisky ulozit
     * @throws IOExcetpion ak sa nepodari zapisat subor 
     */
    
    public static void saveSimulationResults(LinkedList<Statistic> st, String path) throws IOException {
        createXmlDocument();
        
        for (Statistic stat : st) {
            Element statElement = root.addElement( "statistic" );
            statElement.addAttribute( "type", stat.typeToStr());
            statElement.addAttribute( "entityName", stat.getEntityName());
            statElement.addAttribute( "meanValue", String.valueOf(stat.getMeanValue()));
            statElement.addAttribute( "standardDeviation", String.valueOf(stat.getStandardDeviation()));
            statElement.addAttribute( "intervalEstaminationMinimum", String.valueOf(stat.getIntervalEstamination().minimum));  
            statElement.addAttribute( "intervalEstaminationMaximum", String.valueOf(stat.getIntervalEstamination().maximum));      
            root.addText("\n");
        }
        
        saveXmlDocument(path);
    }
    
}
